# @Time : 2023/1/5
# @Author : SDAJAVA

# 一般常用快代理
import urllib.request
import random

url = 'https://www.baidu.com/s?wd=ip'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

request = urllib.request.Request(url=url, headers=headers)

# 使用代理服务器模拟访问
proxies = {
    'http': '192.186.1.1:8080'
}
# 代理池
proxies_pool = [
    {'http': '192.186.1.1:8080'},
    {'http': '192.186.1.1:6666'},
    {'http': '192.186.1.1:2222'},
]
# 从代理池中随机获取
proxies = random.choice(proxies_pool)

headler = urllib.request.ProxyHandler(proxies=proxies)
opener = urllib.request.build_opener(headler)
response = opener.open(request)

content = response.read().decode('utf-8')

with open('baidu_ip.html','w',encoding='utf-8') as fp:
    fp.write(content)
